<template>
  <el-main>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/company' }"
        >企业管理
      </el-breadcrumb-item>
      <el-breadcrumb-item :to="{ path: '/company/list' }"
        >企业列表
      </el-breadcrumb-item>
      <el-breadcrumb-item>企业推送</el-breadcrumb-item>
    </el-breadcrumb>
    <div class="margin-top-20">
      <el-card shadow="never">
        <el-form :model="form" :rules="rules" label-width="80px" ref="form">
          <el-form-item class="m-input-400" label="企业名称" prop="companyName">
            {{ form.companyName }}
          </el-form-item>
          <el-form-item label="设备类型" prop="deviceType">
            <el-radio-group v-model="form.deviceType">
              <el-radio
                :key="item.code"
                :label="item.code"
                v-for="item in device_type"
                >{{ item.label }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item class="m-input-500" label="推送地址" prop="mobile">
            <el-input v-model="form.pushUrl"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button @click="submitForm('form')" type="primary"
              >立即更新
            </el-button>
            <el-button @click="cancelForm">取消</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
  </el-main>
</template>

<script>
export default {
  data() {
    return {
      id: null,
      form: {
        companyId: null,
        companyName: null,
        pushUrl: null,
        deviceType: null
      },
      device_type: []
    };
  },
  mounted() {
    this.id = this.$route.params.id;
    this.info();
    this.load_device_type();
  },
  methods: {
    load_device_type() {
      this.$ajax.support
        .device_type()
        .then(result => {
          if (result.code === 1) {
            this.device_type = result.data;
          }
        })
        .catch(e => {
          console.log(e);
        });
    },
    info() {
      this.$ajax.company
        .push_info(this.id)
        .then(result => {
          if (result.code != 1) {
            this.$message({
              message: result.msg,
              type: "warning"
            });
            this.$router.push("/company/list/");
          } else {
            this.form = result.data;
          }
        })
        .catch(e => {
          console.log(e);
          this.$message({
            message: "系统繁忙，请稍后重试",
            type: "warning"
          });
        });
    },
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          let loadingInstance = this.$loading({ fullscreen: true });
          this.$ajax.company
            .push_info_update({
              companyId: this.id,
              pushUrl: this.form.pushUrl,
              deviceType: this.form.deviceType
            })
            .then(result => {
              if (result.code != 1) {
                this.$message({
                  message: result.msg,
                  type: "warning"
                });
              } else {
                this.$message({
                  type: "success",
                  message: "修改企业推送信息成功!"
                });
                this.$router.push("/company/list/");
              }
            })
            .catch(e => {
              console.log(e);
              this.$message({
                message: "系统繁忙，请稍后重试",
                type: "warning"
              });
            })
            .finally(function() {
              loadingInstance.close();
            });
        } else {
          return false;
        }
      });
    },
    cancelForm() {
      this.$router.push("/company/list/");
    }
  }
};
</script>
